package com.kedacom.ctsp.authority.controller;

import com.kedacom.ctsp.authority.constant.AccessLoggerConstant;
import com.kedacom.ctsp.authority.constant.AuthorizeConstant;
import com.kedacom.ctsp.authority.entity.Area;
import com.kedacom.ctsp.authority.service.AreaService;
import com.kedacom.ctsp.authority.vo.AreaVo;
import com.kedacom.ctsp.authz.Authorize;
import com.kedacom.ctsp.logging.AccessLogger;
import com.kedacom.ctsp.web.controller.AbstractCrudTreeController2;
import com.kedacom.ctsp.web.controller.message.ResponseMessage;
import com.kedacom.ctsp.web.controller.util.QueryParamUtil;
import com.kedacom.ctsp.web.entity.TreeSupportBean;
import com.kedacom.ctsp.web.entity.page.PagerResult;
import com.kedacom.ctsp.web.entity.param.QueryParamEntity;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 区域管理
 */

@ResponseBody
@RequestMapping("area")
@Authorize(resources = AuthorizeConstant.AUTH_AREA)
@AccessLogger(AccessLoggerConstant.Module.AREA)
@Api(tags = "AREA", description = "区域管理")
@Slf4j
public class AuthorityAreaController extends AbstractCrudTreeController2<Area, String, AreaVo, AreaService> {

    @Authorize(resources = AuthorizeConstant.AUTH_AREA_QUERY)
    @Override
    public ResponseMessage<PagerResult<AreaVo>> list(QueryParamEntity param, HttpServletRequest request) {
        return super.list(param, request);
    }

    @Authorize(resources = AuthorizeConstant.AUTH_AREA_UPDATE)
    @Override
    public ResponseMessage<String> updateByPrimaryKey(@PathVariable String id, @RequestBody AreaVo data) {
        return super.patchByPrimaryKey(id, data);
    }

    @Authorize(resources = AuthorizeConstant.AUTH_AREA_ADD)
    @Override
    public ResponseMessage<String> add(@RequestBody AreaVo data) {
        return super.add(data);
    }

    @Authorize(resources = AuthorizeConstant.AUTH_AREA_QUERY)
    @Override
    public ResponseMessage<List<AreaVo>> listAsTree(QueryParamEntity param, HttpServletRequest request) {
        return super.listAsTree(param, request);
    }

    @Authorize(resources = AuthorizeConstant.AUTH_AREA_QUERY)
    @Override
    public ResponseMessage<List<AreaVo>> listAsTree(@PathVariable String id, QueryParamEntity param, HttpServletRequest request) {
        QueryParamUtil.paddingTerms(param, request);
        param.setPaging(false);
        List<Area> results = this.service.selectAsTree(id, param);
        ResponseMessage<List<AreaVo>> result = ResponseMessage.ok(this.entityToModel(results));
        result.exclude(TreeSupportBean.class, new String[]{"parent"});
        return result;
    }


}
